package com.wxn.learncompose.pagers

import android.widget.Toast
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.wxn.learncompose.MyApp

/****
 * 显示dialog弹窗
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AlertDialogPager() {
    var showDialog by remember {
        mutableStateOf(false)
    }
    var showDialog2 by remember {
        mutableStateOf(false)
    }
    Surface(
        shape = MaterialTheme.shapes.medium,
        shadowElevation = 5.dp,
        modifier = Modifier
            .padding(all = 12.dp)
            .clip(RoundedCornerShape(24.dp))
    ) {
        Column {
            Button(onClick = { showDialog = !showDialog }) {
                Text(text = "显示AlertDialog对话框")
            }
            Button(onClick = { showDialog2 = !showDialog2 }) {
                Text(text = "显示Dialog对话框")
            }
        }

        if (showDialog) {
            AlertDialog(
                onDismissRequest = { showDialog = false },
            ) {
                Surface(
                    shape = MaterialTheme.shapes.large,
                    modifier = Modifier
                        .padding(all = 12.dp)
                        .size(width = 280.dp, height = 150.dp)
                        .clip(RoundedCornerShape(6.dp))
                        .shadow(elevation = 5.dp)
                ) {
                    Column(
                        Modifier.padding(12.dp)
                    ) {
                        Text(text = "开启位置服务",
                            style = MaterialTheme.typography.titleSmall,
                            color = Color(0xFF333333),
                            fontWeight = FontWeight.W600,
                            fontSize = TextUnit(18f, TextUnitType.Sp),
                            modifier = Modifier.padding(horizontal = 0.dp, vertical = 4.dp),
                        )

                        Text(text = "这将意味着，我们会给您提供精准的位置服务，并且您将接受关于您订阅的位置信息",
                            style = MaterialTheme.typography.bodySmall,
                            color = Color(0xFF669966),
                            fontWeight = FontWeight.W400,
                            fontSize = TextUnit(14f, TextUnitType.Sp)
                        )

                        Spacer(
                            modifier = Modifier.size(1.dp,  12.dp)
                        )
                        Row {
                            Spacer(modifier = Modifier.weight(1f, true))
                            Button(onClick = {
                                showDialog = false
                            }) {
                                Text(text = "取消")
                            }
                            Spacer(modifier = Modifier.size(8.dp, 1.dp))
                            Button(onClick = {
                                showDialog = false
                                Toast.makeText(MyApp.context(), "确定", Toast.LENGTH_SHORT).show()
                            }) {
                                Text(text = "确定")
                            }
                        }
                    }
                }
            }
        }

        if (showDialog2) {
            Dialog(onDismissRequest = { showDialog2 = false}) {
                Surface(
                    modifier = Modifier
                        .padding(all = 12.dp)
                        .clip(RoundedCornerShape(6.dp))
                        .shadow(elevation = 5.dp)
                ) {
                    Box(modifier = Modifier.size(width = 280.dp, height = 140.dp),
                        contentAlignment = Alignment.Center
                    ) {
                        Column(horizontalAlignment = Alignment.CenterHorizontally) {
                            LinearProgressIndicator()
                            Row(verticalAlignment = Alignment.CenterVertically,
                              ) {
                                Text(text = "loading...")
                            }
                        }
                    }
                }
            }
        }
    }
}
